package com.example.service.impl;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.Constants;
import com.example.entry.OrderForm;
import com.example.entry.OrderGrabbing;
import com.example.entry.Product;
import com.example.entry.User;
import com.example.exception.ServiceException;
import com.example.mapper.OrderGrabbingMapper;
import com.example.mapper.ProductMapper;
import com.example.mapper.UserMapper;
import com.example.service.IOrderFormService;
import com.example.mapper.OrderFormMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author 27223
* @description 针对表【order(订单表)】的数据库操作Service实现
* @createDate 2024-03-06 20:35:07
*/
@Service
public class OrderFormServiceImpl extends ServiceImpl<OrderFormMapper, OrderForm>
    implements IOrderFormService {

    @Autowired
    private OrderFormMapper orderFormMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderGrabbingMapper orderGrabbingMapper;

    /*
    * 用户下单
    * */
    @Override
    public Integer userBuyProduct(OrderForm orderForm) {
        try {
            // 插入到订单表
            orderFormMapper.insertOrderForm(orderForm);
            Integer orderFormId = orderForm.getId();
            // 插入数据到抢单表
            orderGrabbing(orderForm, orderFormId);
            // 减少用户余额
            subUserMoney(orderForm);
            // 修改商品数量
            updateProductNum(orderForm);

            return orderFormId;
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_600, "数据错误!请联系管理员");
        }

    }

    /*
    * 减少用户余额
    * */
    public void subUserMoney(OrderForm orderForm){
        try {
            System.out.println("-------------我是减少用户余额");
            User user = userMapper.selectById(orderForm.getUserId());
            user.setMoney((float) (user.getMoney() - orderForm.getProductMoney()));
            userMapper.updateById(user);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_400,"用户余额操作错误！！");
        }
    }
    /*
    * 修改商品数量
    * */
    public void updateProductNum(OrderForm orderForm){
        try {
            System.out.println("-------------我是修改商品数量");
            Product product = productMapper.selectById(orderForm.getProductId());
            product.setNumber(product.getNumber() - orderForm.getProductNumber());
            productMapper.updateById(product);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_400,"商品数量操作错误！！");
        }
    }

    /*
    * 发布到抢单
    * */
    public void orderGrabbing(OrderForm orderForm, Integer orderFormId){
        // 添加到抢单表中
        try {
            System.out.println("-------------我是发布到抢单");
            OrderGrabbing orderGrabbing = new OrderGrabbing();
            // 处理订单id
            orderGrabbing.setOrderFormId(orderFormId);
            // 处理用户数据
            orderGrabbing.setUserId(orderForm.getUserId());
            orderGrabbing.setNickName(orderForm.getNickName());
            orderGrabbing.setUserAddress(orderForm.getUserAddress());
            orderGrabbing.setUserPhone(orderForm.getUserPhone());
            // 处理商家数据
            orderGrabbing.setProductId(orderForm.getProductId());
            orderGrabbing.setProductName(orderForm.getProductName());
            orderGrabbing.setProductPrice(orderForm.getProductMoney());
            orderGrabbing.setCommission(orderForm.getProductMoney() * 0.25);
            orderGrabbing.setProductAddress(orderForm.getProductAddress());
            orderGrabbing.setOrderTime(orderForm.getCreateTime());
            orderGrabbingMapper.insert(orderGrabbing);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_400,"抢单数据操作错误！！");
        }
    }


    /*
    * 用户取消订单
    *
    * */
    @Override
    public void userCancelOrder(OrderForm orderForm) {

        try {
            // 改变订单状态
            orderFormMapper.updateById(orderForm);
            // 退回用户金额
            returnUserMoney(orderForm);
            // 退回商品数量
            returnProductNum(orderForm);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_600, "取消订单错误");
        }
    }

    /*
     * 用户使用支付宝下单
     * */
    @Override
    public Integer useAlipayPayment(OrderForm orderForm) {
        int insert = orderFormMapper.insert(orderForm);
        return insert;
    }

    /*
    * 取消订单，退回余额
    * */
    public void returnUserMoney(OrderForm orderForm){
        try {
            // 查询用户信息
            User user = userMapper.selectById(orderForm.getUserId());
            user.setMoney((float) (user.getMoney()+orderForm.getProductMoney()));
            userMapper.updateById(user);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_500, "退回余额失败");
        }
    }

    /*
    * 取消订单，退回商品数量
    * */
    public void returnProductNum(OrderForm orderForm){
        try {
            // 查询商品信息
            Product product = productMapper.selectById(orderForm.getProductId());
            product.setNumber(product.getNumber() + 1); // 改变商品数量
            productMapper.updateById(product);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_500, "退回商品数量失败");
        }
    }
}




